package com.jcrj.action.personalOffice;

import com.jcrj.action.BaseAction;
import com.jcrj.entity.ODaily;
import com.jcrj.entity.OUser;
import com.jcrj.service.PersonalofficeService;
import com.jcrj.util.PoiExcelUtil;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class DailyManagerAction extends BaseAction {
	private ODaily oDaily;
	private String content;
	private String weather;
	private List<ODaily> dailyList;
	private PersonalofficeService personalofficeService;
	private String[] dalilyId;
	private int id;
	private String name;
	private long pageCount;
	private long totalCount;
	private Date createTime;
	private String userName;

	public String updateDalily() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			ODaily od = new ODaily();
			od.setId(Integer.valueOf(this.id));
			od.setUserId(user.getId());
			od.setUserName(user.getName());
			od.setContent(this.content);
			od.setCreateTime(this.createTime);
			od.setWeather(this.weather);
			this.personalofficeService.updateDalily(od);
			super.getResponse().getWriter().write("success");
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String dalilyUpdate() {
		this.oDaily = this.personalofficeService.getDailyNew(this.id);
		return "success";
	}

	public String dalilyExport() {
		OutputStream outputStream = null;
		try {
			String tableName = "个人日志";
			String excelName = URLEncoder.encode(tableName, "UTF-8");
			super.getResponse().setHeader("Content-Disposition",
					"attachment; filename=" + excelName + ".xls");
			String[] x = { "时间", "天气", "内容" };
			Object[][] data = this.personalofficeService
					.getDailyExportList(this.dalilyId);
			HSSFWorkbook excel = PoiExcelUtil.createExcelData(tableName, x,
					data);
			outputStream = super.getResponse().getOutputStream();

			excel.write(outputStream);
			outputStream.flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				if (outputStream != null)
					outputStream.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				if (outputStream != null)
					outputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String deleteDalily() {
		try {
			this.personalofficeService.deleteDaily(this.dalilyId);
			super.getResponse().getWriter().write("success");
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String dailyManager() {
		OUser user = (OUser) super.getSession().get("userInfo");
		this.totalCount = this.personalofficeService.countDaily(
				user.getId().intValue(), this.name, super.getStartDate(),
				super.getEndDate()).longValue();
		this.pageCount = ((this.totalCount - 1L) / 10L + 1L);
		if (super.getCurrentPage() > this.pageCount) {
			super.setCurrentPage(Long.valueOf(this.pageCount).intValue());
		}
		this.dailyList = this.personalofficeService.getDaily(user.getId()
				.intValue(), this.name, super.getCurrentPage(), super
				.getStartDate(), super.getEndDate());
		return "success";
	}

	public String emplyeeDailyManage() {
		OUser user = (OUser) super.getSession().get("userInfo");
		String users = this.personalofficeService.getUserEmplyee(user.getId()
				.toString());
		this.totalCount = this.personalofficeService.countDailyEmplyee(users,
				this.name, this.userName, super.getStartDate(),
				super.getEndDate()).longValue();
		this.pageCount = ((this.totalCount - 1L) / 10L + 1L);
		if (super.getCurrentPage() > this.pageCount) {
			super.setCurrentPage(Long.valueOf(this.pageCount).intValue());
		}
		this.dailyList = this.personalofficeService.emplyeeDailyList(users,
				this.name, this.userName, super.getStartDate(),
				super.getEndDate(), super.getCurrentPage());
		return "success";
	}

	public String dalilySave() {
		setCreateTime(new Date());
		return "success";
	}

	public String dalilyAdd() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			ODaily od = new ODaily();
			od.setUserId(user.getId());
			od.setUserName(user.getName());
			od.setContent(this.content);
			od.setCreateTime(this.createTime);
			od.setWeather(this.weather);
			this.personalofficeService.addDaily(od);
			super.getResponse().getWriter().write("success");
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public PersonalofficeService getPersonalofficeService() {
		return this.personalofficeService;
	}

	public void setPersonalofficeService(
			PersonalofficeService personalofficeService) {
		this.personalofficeService = personalofficeService;
	}

	public ODaily getODaily() {
		return this.oDaily;
	}

	public void setODaily(ODaily daily) {
		this.oDaily = daily;
	}

	public String getContent() {
		return this.content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public List<ODaily> getDailyList() {
		return this.dailyList;
	}

	public void setDailyList(List<ODaily> dailyList) {
		this.dailyList = dailyList;
	}

	public String[] getDalilyId() {
		return this.dalilyId;
	}

	public void setDalilyId(String[] dalilyId) {
		this.dalilyId = dalilyId;
	}

	public String getWeather() {
		return this.weather;
	}

	public void setWeather(String weather) {
		this.weather = weather;
	}

	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public long getPageCount() {
		return this.pageCount;
	}

	public void setPageCount(long pageCount) {
		this.pageCount = pageCount;
	}

	public long getTotalCount() {
		return this.totalCount;
	}

	public void setTotalCount(long totalCount) {
		this.totalCount = totalCount;
	}

	public Date getCreateTime() {
		return this.createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public String getUserName() {
		return this.userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}
}